package com.kexilo.core.common.utils;

import java.security.MessageDigest;

/**
 * MD5工具类
 * 
 * @author Kexilo
 */
public class MD5Utils {
    
    /**
     * 计算字符串的MD5值
     * 
     * @param str 字符串
     * @return MD5值
     */
    public static String md5(String str) {
        if (str == null) {
            return null;
        }
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(str.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
